package mipo.top.admin.controller;

import mipo.top.base.Dto.UserInfo;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.admin.domain.Department;
import mipo.top.admin.query.DepartmentQuery;
import mipo.top.admin.service.IDepartmentService;
import mipo.top.users.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    public List<Department> sad(){
        return departmentService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.insert(department);
            }else{
                departmentService.updata(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        departmentService.delete(id);
    }

    @PostMapping
    public PageUtil<Department> asp(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request) throws Exception {
        Logininfo loginUser = LoginContext.getLoginUser(request);
        System.out.println(loginUser);
        return departmentService.queryDate(departmentQuery);
    }
    @PatchMapping
    public void bacthDel(@RequestBody List<Long> ids){
        departmentService.bacthDel(ids);
    }

    @PostMapping("/Tree")
    public List<Department> wds(){
        return departmentService.deptTree();
    }

}
